package com.taikang.dao;

import com.taikang.dao.base.DefaultCommonDaoImpl;
import com.taikang.entity.ProductInfo;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.stereotype.Repository;

/**
 * Created by zhai on 16/5/14.
 */
@Repository
public class PushDao extends DefaultCommonDaoImpl<ProductInfo> {

    private SqlCallback pojoSqlCallback = null;
    private SqlCallback listSqlCallback = null;

    public Object getProductInfo(Integer productId, Integer page, Integer size) {
        Sql sql = Sqls.create(getDao().sqls().get("getProduct"));
        Criteria cri = Cnd.cri();
        if(null != productId) {
            cri.where().and("a.ProductID", "=", productId);
        }
        SqlCallback callback = null;
        sql.setCondition(cri);
        if(null == productId) {
            if(null == listSqlCallback)
                listSqlCallback = createSqlCallBack(true, ProductInfo.class);
            callback = listSqlCallback;
        } else {
            if(null == pojoSqlCallback)
                pojoSqlCallback = createSqlCallBack(false, ProductInfo.class);
            callback = pojoSqlCallback;
        }
        sql.setCallback(callback);
        return null == productId ? listPage(page, size, sql) : ((Sql) execute(sql)).getContext().getResult();
    }

}
